var app = {};
Ext.onReady(function() {
	Ext.QuickTips.init();
	var viewport = new Ext.Viewport({
		layout : 'border',
		renderTo : 'viewport',
		items : [ new Ext.BoxComponent({
			region : 'north',
			height : 60,
			margins : '0 0 0 0',
			contentEl : 'top_panel'
		}), {
			region : 'east',
			title : '其他工具',
			collapsible : true,
			collapsed : true,
			split : true,
			width : 175,
			margins : '0 2 2 0',
			layout : 'fit',
			items : new Ext.TabPanel({
				border : false,
				activeTab : 1,
				tabPosition : 'bottom',
				items : [ {
					html : '<p>工具1</p>',
					title : '类别1',
					autoScroll : true
				}, new Ext.grid.PropertyGrid({
					title : '类别2',
					closable : false,
					source : {
						"菜单名称" : "Properties Grid",
						"菜单编码" : false,
						"图标" : true,
						"是否启用" : false,
						"created" : new Date(Date.parse('10/15/2006')),
						"tested" : false,
						"version" : 0.01,
						"borderWidth" : 1
					}
				}) ]
			})
		}, {
			region : 'west',
			id : "leftPanel",
			title : '系统管理',
			iconCls : "tab-home",
			split : true,
			width : 230,
			minSize : 175,
			maxSize : 400,
			collapsible : true,
			margins : '0 0 2 2',
			items : [ {
				id : 'leftOpTabPanel',
				xtype : 'tabpanel',
				border : false,
				activeItem : 0,
				style : "height:100%",
				items : [ leftOpTabTree, new Ext.tree.TreePanel({
					id : 'leftCutTabTree',
					title : '快捷方式',
					autoScroll : true,
					animate : true,
					containerScroll : true,
					border : -1,
					bodyCssClass : "tree-bg",
					iconCls : 'tab-sys-tab-shortcut',
					rootVisible : false,
					root : {
						id : 'mycts',
						root : true,
						text : '我的快捷菜单',
						expand : true
					},
					loader : new Ext.tree.TreeLoader({
						url : $path + "/shortCut/getShortCut.do?userId=" + $User.userId,
						requestMethod : "GET",
						createNode : function(attr) {
							attr.text = attr.text || attr.resName;
							attr.leaf = !attr.root;
							return K.Ext.tree.TreeLoader.prototype.createNode.call(this, attr);
						}
					}),
					listeners : {
						click : function(node) {
							if (node.isLeaf() || true) {
								var id = "mtb_" + node.code;
								var tab = app.mainTabPanel.getItem(id);
								if (tab == null && node.attributes.type == "M") {
									tab = new Ext.Panel({
										id : "mtb_" + node.id,
										bid : node.id,
										title : node.text,
										iconCls : node.attributes.iconCls || "tab-1",
										tabTip : node.text,
										closable : true,
										html : K.wrapIframe($path + "/requestStage.do?code=" + node.attributes.code)
									});
									app.mainTabPanel.add(tab);
									app.mainTabPanel.activate(tab);
								}
							}
						}
					}
				}) ]
			} ]
		}, new Ext.TabPanel({
			id : "mainTabPanel",
			region : 'center',
			margins : '0 -3 2 -3',
			activeTab : 0,
			plugins : [ new Ext.ux.TabCloseMenu(), new Ext.ux.TabScrollerMenu({
				maxText : 15,
				pageSize : 5
			}) ],
			items : [ {
				title : '系统首页',
				iconCls : "tab-home",
				width : "100%",
				html : K.wrapIframe({
					url : $path + "/requestStage.do?code=welcome",
					id : "ifHomeTab"
				}),
				closable : true
			} ]

		}) ]
	});

	app.viewport = viewport;
	app.leftTabPanel = Ext.getCmp("leftOpTabPanel");
	app.leftOpTabTree = leftOpTabTree;
	app.leftCutTabTree = Ext.getCmp("leftCutTabTree");
	app.leftOpTabTree.setHeight(app.leftTabPanel.getHeight() - 26);
	app.leftCutTabTree.setHeight(app.leftTabPanel.getHeight() - 26);

	app.mainTabPanel = Ext.getCmp("mainTabPanel");

	app.viewport.doLayout();

	setTimeout(function() {
		Ext.get('loading').remove();
		Ext.get('loading-mask').fadeOut({
			remove : true
		});
	}, 250);
});

var rootNode = new Ext.tree.TreeNode({
	expanded : true,
	text : '系统菜单',
	id : 'ROOT_ID'
});

var leftOpTabTree = new Ext.tree.TreePanel({
	title : "管理菜单",
	useArrows : false,
	autoScroll : true,
	animate : true,
	containerScroll : true,
	border : -1,
	rootVisible : false,
	// bodyStyle : "background:#eee;",
	bodyCssClass : "tree-bg",
	root : rootNode,
	iconCls : "tab-sys-tab-menu ",
	listeners : {
		click : function(node) {
			if (node.isLeaf() || true) {
				var id = "mtb_" + node.code;
				var tab = app.mainTabPanel.getItem(id);
				if (tab == null && node.attributes.type == "M") {
					tab = new Ext.Panel({
						id : "mtb_" + node.id,
						bid : node.id,
						title : node.text,
						iconCls : node.attributes.iconCls || "tab-1",
						tabTip : node.text,
						closable : true,
						html : K.wrapIframe($path + "/requestStage.do?code=" + node.attributes.code)
					});
					app.mainTabPanel.add(tab);
					app.mainTabPanel.activate(tab);
				}
			}
		},
		expandnode : function(node) {
			if (!node.isRoot) {
				app.res.getResByCode(node.attributes.code).expanded = true;
			}
		},
		collapsenode : function(node) {
			if (!node.isRoot) {
				app.res.getResByCode(node.attributes.code).expanded = false;
			}
		}
	}
});

// resources
app.res = {
	data : null,
	idMap : {},
	cusCodeMap : {},
	codeMap : {},
	childrensMap : {},
	hiddenData : [],
	visiableData : [],
	init : function(resData) {
		this.data = resData;
		// 数据处理
		for ( var i = 0, node; i < this.data.length; i++) {
			node = this.data[i];
			if (node.visiable && (node.type == "S" || node.type == "M")) {
				this.visiableData.push(node);
			} else {
				this.hiddenData.push(node);
			}
			this.cusCodeMap[node.cusCode] = node;
		}

		// 资源显示处理
		for ( var i = 0, node; i < this.visiableData.length; i++) {
			node = this.visiableData[i];
			node.text = node.name;
			this.idMap[node.id] = this.codeMap[node.code] = node;
			node.iconCls = node.iconCls || "res_icon_" + node.type;
			node.leaf = (node.leaf == 1);
			if (!node.leaf) {
				node.children = this.getChildrensByCode(node.code);
				node.leaf = node.children.length == 0;
			}
		}
	},
	getResByCode : function(code) {
		return this.codeMap[code];
	},
	getResById : function(code) {
		return this.idMap[code];
	},
	eachResources : function(resources, fn) {
		for ( var i = 0; i < resources.length; i++) {
			if (!fn(i, resources[i])) {
				break;
			}
		}
	},
	eachChildrens : function(code, fn) {
		var childrens = this.getChildrensByCode(code);
		if (!childrens || childrens.length == 0) {
			return;
		}

		for ( var i = 0; i < childrens.length; i++) {
			if (fn(i, childrens[i])) {
				break;
			}
		}
	},
	eachAllResources : function(fn) {
		for ( var i = 0; i < this.visiableData.length; i++) {
			if (fn(i, this.visiableData[i])) {
				break;
			}
		}
	},
	getResByType : function(type, grade) {
		var rs = [];
		this.eachAllResources(function(i, v) {
			if (v.type == type) {
				if (v.grade == grade) {
					rs.push(v);
				}
			}
		});
		return rs;
	},
	getModules : function() {
		return this.getResByType("S", 1);
	},
	getChildrensByCode : function(code) {
		var me = this;
		if (!me.childrensMap[code]) {
			var childrens = [];
			this.eachAllResources(function(i, v) {
				if (v.parentCode == code) {
					childrens.push(v);
				}
			});
			me.childrensMap[code] = childrens;
		}
		return me.childrensMap[code];
	},
	getResByCusCode : function(cusCode) {
		return this.cusCodeMap[cusCode];
	}
};

// operate
app.bindResourceToLeftOpTabTree = function(res) {
	if (typeof (res) != "object") {
		res = app.res.getResByCode(res);
	}

	var rootNode = app.leftOpTabTree.getRootNode();
	var childNodes = rootNode.childNodes;
	if (childNodes && childNodes.length != 0) {
		for ( var i = 0, length = childNodes.length; i < length; i++) {
			childNodes[0].remove();
		}
	}

	var nodes = res;
	if (res.code == "0") {
		nodes = app.res.getChildrensByCode(res.code);
	}

	if (nodes.length != 0) {
		rootNode.appendChild(nodes);
		if (rootNode.firstChild && !rootNode.firstChild.isExpanded()) {
			rootNode.firstChild.expand();
		}
	}
};
